package com.ruoyi.service.impl;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.*;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.AIService;
import com.ruoyi.service.IT03Service;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.uitity.MinioUtil;
import com.ruoyi.uitity.WxMessageProducer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.service.IT06Service;
import com.ruoyi.service.IT07Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@Service
public class AiServiceImpl implements AIService {

    private com.github.benmanes.caffeine.cache.Cache<String, String> cache = Caffeine.newBuilder()
            .maximumSize(1000) // 设置最大容量
            .expireAfterWrite(10, TimeUnit.MINUTES) // 设置过期时间
            .build();
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IT06Service t06Service;
    @Autowired
    private IT07Service t07Service;
    @Autowired
    private WxMessageProducer producer;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private IT03Service t03Service;

    @Autowired
    private MinioUtil minioUtil;

    @Transactional
    @Override
    public AjaxResult wenda(String str, HttpServletRequest request) {
        str = str.toString();
        LoginUser loginUser = tokenService.getLoginUser(request); // 确保tokenService和getLoginUser方法存在
        Long userId = loginUser.getUserId();
        T06 t06 = t06Service.selectT06ByF0601(userId);
        T07 t07 = new T07();
        t07.setF0601(userId);
        t07.setF0703(str);
        Date date = new Date();
        t07.setF0704(date);
        t07.setF0705("");
        String answer = cache.getIfPresent(str);
        if (answer != null) {
            t07.setF0706(0L);
            t07Service.insertT07(t07);
            return AjaxResult.success(answer);
        }
        String result = (String) redisTemplate.opsForValue().get(str+userId);
        if (result != null) {
            Wxmessage wxmessage=new Wxmessage(userId,result);
            cache.put(str, result);
            t07.setF0706(0L);
            t07Service.insertT07(t07);
            return AjaxResult.success(result);
        }
        if (t06.getF0611()<=0){
            return new AjaxResult(402, "请先充值");
        }
        try {
            Future<String> response = producer.sendMessage(str,userId);
            cache.put(str+userId,response.get());
            redisTemplate.opsForValue().set(str+userId, response.get(), 10, TimeUnit.MINUTES);
            int count= str.length()+response.get().length();
            Long compId = t06.getF0501();
            T03 t03 =t03Service.selectT03ByF0301(compId) ;
            t03.setF0312(t03.getF0312()+count);
            t03.setF0311(t03.getF0311()+1L);
            t03Service.updateT03(t03);
            if(count>t06.getF0611()){
                t06.setF0611(0L);
            }else{
                t06.setF0605(t06.getF0605()+count);
                t06.setF0611(t06.getF0611()-count);
            }
            t06Service.updateT06(t06);
            t07.setF0706((long) count);
            t07Service.insertT07(t07);
            return AjaxResult.success(response.get());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(500, "服务器错误");
        }
    }
    @Autowired
    private SysRegisterService registerService;

    @Transactional
    @Override
    public String register(RegisterBody  registerBodyBody) {
        String msg=registerService.register(registerBodyBody);
        SysUser user =userService.selectUserByUserName(registerBodyBody.getUsername());
        Long userId = user.getUserId();
        System.out.println(userId+"dsfdefewfewfew");
        if(!t06Service.checkUserNameUnique(registerBodyBody.getUsername())){
            return  "用户名重复";
        }
        T06 t06 = new T06();
        Date date = new Date();
        t06.setF0601(userId);
        t06.setF0615(date);
        t06.setF0616("DEFULTIMG");
        t06.setF0611(Sendnumber.SENDNUMBER);
        t06.setF0603(registerBodyBody.getUsername());
        t06.setF0608(SecurityUtils.encryptPassword(registerBodyBody.getPassword()));
        System.out.println(t06.toString());
        int s =t06Service.insertT06(t06);
        if(msg.isEmpty()){
            return "";
        }
        return msg;
    }

    @Override
    public boolean updateUserImg(MultipartFile file, Long userId) {
        T06 t06 = t06Service.selectT06ByF0601(userId);
        String filename = UUID.randomUUID().toString();
        String oldname = t06.getF0616();
        t06.setF0616(filename);
        t06Service.updateT06(t06);
        try {
            minioUtil.upload(file, filename);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
